﻿using Cinchoo.Core;
using Cinchoo.Core.Configuration;
using Cinchoo.Core.Diagnostics;
using Cinchoo.Core.Shell;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Cinchoo.Core.Windows.Forms;

namespace ChoBulkFileProcessor
{
    [ChoApplicationHost]
    public class AppHost : ChoApplicationHost
    {
        public const string AppFileExt = ".blk";
        private string _defaultBalloonTipText = null;

        [STAThread]
        public static void Main(string[] args)
        {
            ChoApplication.Run(args);
        }

        protected override void ApplyGlobalApplicationSettingsOverrides(ChoGlobalApplicationSettings obj)
        {
            obj.TrayApplicationBehaviourSettings.TurnOn = true;
            obj.TrayApplicationBehaviourSettings.HideTrayIconWhenMainWindowShown = true;
            obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayHelpMenuItem = false;
            obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayAboutMenuItem = false;
            obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayShowMainWndMenuItem = true;
            obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayShowInTaskbarMenuItem = false;

            obj.ApplicationBehaviourSettings.SingleInstanceApp = false;
            obj.ApplicationBehaviourSettings.ActivateFirstInstance = true;
        }

        protected override void OnWindowMinimize(ChoNotifyIcon notifyIcon)
        {
            ChoApplication.NotifyIcon.ShowBalloonTip(_defaultBalloonTipText, 500);
        }

        protected override void OnStart(string[] args)
        {
            _defaultBalloonTipText = "{0} is running...".FormatString(ChoGlobalApplicationSettings.Me.ApplicationName);

            try
            {
                ChoShellExtension.Register();
                ChoTrace.WriteLine("Shell Extensions registered successfully.");
            }
            catch (Exception ex)
            {
                ChoTrace.WriteLine("Failed to register Shell Extensions. {0}".FormatString(ex.Message));
            }
            try
            {
                ChoShellFileAssociation.Register();
                ChoTrace.WriteLine("File Associations registered successfully.");
            }
            catch (Exception ex)
            {
                ChoTrace.WriteLine("Failed to register File Associations. {0}".FormatString(ex.Message));
            }

            if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
            {
                ChoAppCmdLineArgs cmdLineArgs = new ChoAppCmdLineArgs();
                cmdLineArgs.StartProcessing();
            }
            base.OnStart(args);
        }

        public override object MainWindowObject
        {
            get
            {
                ChoFileAssociationCmdLineArgs cmd = new ChoFileAssociationCmdLineArgs();
                if (cmd.IsAppFile)
                    return new MainWindow(cmd.SettingsFilePath);
                else if (cmd.IsPlugInFile)
                {
                    ChoPlugInEditorSettings plugInEditorSettings = new ChoPlugInEditorSettings();
                    plugInEditorSettings.AfterConfigurationObjectLoaded += plugInEditorSettings_AfterConfigurationObjectLoaded;

                    return new ChoPlugInEditor(cmd.SettingsFilePath);
                }
                else
                    return new MainWindow();
            }
        }

        public override object ApplicationObject
        {
            get
            {
                return App.Default;
            }
        }

        private void plugInEditorSettings_AfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
        {
            ChoPlugInEditorSettings plugInEditorSettings = sender as ChoPlugInEditorSettings;
            if (plugInEditorSettings == null) return;

            plugInEditorSettings.NewPlugInDefFileVisible = true;
            plugInEditorSettings.OpenPlugInDefFileVisible = true;
            plugInEditorSettings.SaveAsPlugInDefFileVisible = true;
            plugInEditorSettings.SavePlugInDefFileVisible = true;
        }

        protected override void AfterNotifyIconConstructed(ChoNotifyIcon ni)
        {
            ni.Text = "Bulk File Processor";
        }

        public void ShowMainWindow()
        {
            var a = ApplicationObject;
            var wnd = MainWindowObject;
            if (wnd is Window)
            {
                ChoWindowsManager.HideConsoleWindow();
                ((Window)wnd).ShowDialog();
            }
        }
    }

    [ChoShellExtension]
    public class ShellExt
    {
        [ChoShellExtensionContextMenu("Folder", MenuText = "Bulk file processing...", DefaultArgPrefix="/d:")]
        public static void ProcessFiles(string[] args)
        {
            new AppHost().ShowMainWindow();
        }
    }

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static readonly App Default = new App();

        public App()
        {
            InitializeComponent();
        }
    }
}
